import request from "./request.ts";
import { GET_REFRESH_TOKEN } from "./utils.ts";
import type { AxiosRequestConfig } from "axios";

let promise: Promise<boolean> | null = null;
export async function tokenRefresh() {
  if (promise) return promise;

  promise = new Promise(async (resolve) => {
    const res = await request.get("/refresh", {
      headers: {
        XAuthorization: GET_REFRESH_TOKEN(),
      },
      __hasRefresh: true,
    });
    // 判断刷新token请求是否成功
    resolve(res.code === 200);
  });
  promise.finally(() => {
    promise = null;
  });
}
export function isRefresh(
  config: AxiosRequestConfig & { __hasRefresh?: boolean }
) {
  return !!config.__hasRefresh;
}
